using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEngine;

public class Grenade : MonoBehaviour
{
    private Rigidbody2D rd;

    private Animator anim;

    private bool is_Touched = false;
    private float Boomdis = 4.0f;
    private int boomdam = 5;

    private void Start()
    {
        rd = GetComponent<Rigidbody2D>();
        
        anim = GetComponent<Animator>(); 

        Init();
    }

    private void Update()
    {
        if (!is_Touched)
        {
            rd.transform.Rotate(new Vector3(0, 0, 1), Space.Self);
        }
    }

    private void Init()
    {
        anim.enabled = false;

        Transform player = GameObject.FindGameObjectWithTag("Player").transform;
        
        if(player.rotation.y == 0)
        {
            rd.AddForce(new Vector2(200f, 300f));
        }
        else
        {
            rd.AddForce(new Vector2(-200f, 300f));
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Player" || other.tag == "Grenade")
        {
            return;
        }
        else if (other.tag == "Enemy" || other.tag == "Boss" || other.tag == "Bullet" || other.tag == "Ground")
        {
            HurtEnemys();
        }
        is_Touched = true;

        rd.velocity = Vector2.zero;
        rd.transform.rotation = Quaternion.Euler(0, 0, 0);
        gameObject.GetComponent<Rigidbody2D>().gravityScale = 0;
        anim.enabled = true;
        Soundmgr.Instance.PlayMusicByName("GrenadeExplosion", transform.position, 1f);
    }

    public void DestroyBoom()
    {
        GameObject.Destroy(gameObject);
    }

    public GameObject[] GetBoomEnemys(float dis)
    {
        GameObject[] enemys = GameObject.FindGameObjectsWithTag("Enemy");
        Transform gren = this.transform;
        if(enemys == null)
        {
            return null;
        }

        List<GameObject> list = new List<GameObject>();
        for (int i = 0; i < enemys.Length; i++)
        {
            float d = Vector2.Distance(enemys[i].transform.position, gren.transform.position);
            if (d < dis)
            {
                list.Add(enemys[i]);
            }
        }
        return list.ToArray();
    }

    public GameObject[] GetBoomBosses(float dis)
    {
        GameObject[] bosses = GameObject.FindGameObjectsWithTag("Boss");
        Transform gren = this.transform;
        if (bosses == null)
        {
            return null;
        }

        List<GameObject> list = new List<GameObject>();
        for (int i = 0; i < bosses.Length; i++)
        {
            float d = Vector2.Distance(bosses[i].transform.position, gren.transform.position);
            if (d < dis)
            {
                list.Add(bosses[i]);
            }
        }
        return list.ToArray();
    }

    public void HurtEnemys()
    {
        GameObject[] enemys = GetBoomEnemys(Boomdis);
        GameObject[] bosses = GetBoomBosses(Boomdis);
        if (enemys != null && enemys.Length != 0) 
        {
            foreach (var enemy in enemys)
            {
                enemy.GetComponent<Enemy>().Hurt();
            }
        }
        if(bosses != null && bosses.Length != 0)
        {
            foreach(var boss in bosses)
            {
                boss.GetComponent<Boss>().Hurt(boomdam);
                
            }
            
        }
    }
}
